package j2metris;

import javax.microedition.lcdui.Image;

/**
 * Represents the game field into which the pieces drop.
 */
public class Well
{
    private final Image[][] coordinates;
    private final int width;
    private final int height;
    private final Image backgroundUnitImage;

    public Well(int x, int y, Image bgImage)
    {
        width = x;
        height = y;
        coordinates = new Image[x][y];
        backgroundUnitImage = Image.createImage(bgImage);
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public Image getBackgroundImage()
    {
        return backgroundUnitImage;
    }

    public Image getImage(int x, int y)
    {
//        if(J2metris.DEBUG) System.err.println("WELL.GETIMAGE(" + x + ", " + y + ")");
        if(coordinates[x][y] == null) return null;
        else return Image.createImage(coordinates[x][y]);
    }

    public void clearLine(int row)
    {
        for(int i = 0; i < width; ++i)
        {
            for(int ii = row; ii > 0; --ii)
            {
                coordinates[i][ii] = coordinates[i][ii - 1] == null ?
                    null :
                    Image.createImage(coordinates[i][ii - 1]);
            }
        }
    }

    public void lockPiece(ControllablePiece p, Coordinate position)
    {
        Coordinate[] rotationState = p.getRotationState();
        for(int i = 0; i < rotationState.length; ++i)
        {
            Coordinate combinedPosition = position.add(rotationState[i]);
            coordinates
                    [combinedPosition.getX()]
                    [combinedPosition.getY()]
                    = p.getUnitImage();
        }
    }

    private boolean areObstructed(Coordinate[] locations, Coordinate interval)
    {
        {
            for(int i = 0; i < locations.length; ++i)
            {
                if(isObstructed(locations[i].add(interval)));
                    return true;
            }
            return false;
        }
    }

    public boolean isObstructed(Coordinate c)
    {
        if(
                c.isFurtherLeftThan(0) ||
                c.isFurtherRightThan(width - 1) ||
                c.isHigherThan(0) ||
                c.isLowerThan(height - 1))
            return true;
        else return coordinates[c.getX()][c.getY()] != null;
    }

    public boolean areObstructed(Coordinate[] locations)
    {
        for(int i = 0; i < locations.length; ++i)
        {
            if(isObstructed(locations[i])) return true;
        }
        return false;
    }

    public boolean isObstructed(Coordinate c, Direction.TranslationDirection d)
    {
        return isObstructed(c.add(d.getCoordinate()));
    }

    public boolean areObstructed(Coordinate[] c, Direction.TranslationDirection d)
    {
        for(int i = 0; i < c.length; ++i)
            if(isObstructed(c[i], d))
                return true;
        return false;
    }

    public boolean rowIsFull(int position)
    {
        for(int i = 0; i < width; ++i)
        {
            if(coordinates[i][position] == null) return false;
        }
        return true;
    }
}
